home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18109 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: grantp@usa.pipeline.com(Pete Grant)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question about abstract base class
  5. Date: 18 Apr 1996 21:51:24 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4l6dgs$bno@news1.h1.usa.pipeline.com>
  8. References: <4l3u1s$j80@hermes.acs.unt.edu>
  9. NNTP-Posting-Host: 38.8.61.11
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete Grant)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Apr 17, 1996 23:15:08 in article <Question about abstract base class>,
  16. 'cc0003@jove.acs.unt.edu (Chen-five Chi)' wrote: 
  17.  
  18.  
  19. >I just wrote a simple program like this: 
  20. >class shape 
  21. >{ 
  22. >public: 
  23. >    virtual void print() const = 0; 
  24. >........ 
  25. >} 
  26. >clase TwoDimensionObject: public shape 
  27. >{ 
  28. >public: 
  29. >    virtual void area() const = 0; 
  30. >... 
  31. >} 
  32. >class Square: public TwoDimensionObject 
  33. >{ 
  34. >private: 
  35. >    int side; 
  36. >public: 
  37. >    virtual void area() 
  38.                             ^^^^^ 
  39.   You need const here also. 
  40. >    { 
  41. >       cout << "Square area: " << (side * side); 
  42. >    } 
  43. >} 
  44. >... 
  45. >void main() 
  46. >{ 
  47. >    Square s1; 
  48. >.... 
  49. >} 
  50. >============================== 
  51. >And I get a compiler error in BC 4.51 said "s1 is abstract base class...."
  52.  
  53. >After I move the keyword "const", the program runs normally without any  
  54. >problem.  
  55. area() and area () const are two different functions.  Your base class 
  56. has two pure virtual functions which are not overridden in derived class: 
  57. area(), as already pointed out, and print(), which is inherited from 
  58. shape.  You must define matching nonpure functions in Square before 
  59. you can instantiate an object of that kind. 
  60.  
  61. >I would like to know 
  62. >"Why I Could not put 'const' in the ABC? and the different these two? 
  63. The const specifier declares to the compiler that the function 
  64. does not modify any of the instance variables (members), nor does 
  65. it call any non-const member functions involving the object. 
  66. -- 
  67. Pete Grant 
  68. Kalevi, Inc. 
  69. Software Engineering & development
  70.